﻿package UIComponents
{
	import Classes.GameUtils.MyTipSprite;
	import Classes.GameUtils.ToolTip_Sprite;
	import Classes.staticinfo.Language;
	
	import MyComponent.MyButton;
	
	import fl.events.ComponentEvent;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.text.TextField;
	
	public class UpgradeItemPanel extends Sprite
	{
		private var bqBtn:MyButton;
		public var upmb:DisplayObject;
		private var buqiTxt:TextField;
		private var buydc:DisplayObjectContainer;
		
		private var oldTxtColor:uint;
		
		public function UpgradeItemPanel()
		{
			init();
		}
		
		private function init():void
		{
			this.bqBtn=this.getChildByName("buqiBtn") as MyButton;
			this.bqBtn.visible=false;
			
			this.buqiTxt=this.getChildByName("buqiTxt") as TextField;
			this.buqiTxt.visible=false;
			this.buqiTxt.mouseEnabled=false;
			
			this.getChildByName("lvIcon").visible=false;
			
			this.upmb=this.getChildByName("upmb");
			
			this.buydc=this.getChildByName("buydc") as DisplayObjectContainer;
			this.buydc.visible=false;
			(this.buydc.getChildByName("buyExchangeItem") as MyButton).addEventListener(ComponentEvent.BUTTON_DOWN,buySingle);
			
			this.oldTxtColor	=	(this.getChildByName("numTxt") as TextField).textColor;
			
			addLister();
		}
		
		private function addLister():void
		{
			this.bqBtn.addEventListener(ComponentEvent.BUTTON_DOWN,bqFun);
		}
		
		private function bqFun(e:ComponentEvent):void
		{
			//点击补齐按钮触发的逻辑
			this.shopObj.sendBuyCommand(pil);
		}
		private function buySingle(e:ComponentEvent):void
		{
			//点击购买按钮触发的逻辑
			this.shopObj.sendBuyCommand();
		}
		
		private var shopObj:IGD_ShopItem;
		private var pil:int=1;
		public function setUpItemData(obj:Object):Boolean
		{
			reset();
			var tmpFlag:Boolean=true;
			this.upmb.visible=false;
			this.getChildByName("lvIcon").visible=false;
			this.getChildByName("userJb").visible=false;
			var moenytype:String=FlatInfo.isOPenQQVip?Language.Qdian:Language.ob;
			if(Number(obj.countSecondType)==102)
			{
				this.bqBtn.visible=true;
				this.buqiTxt.visible=true;
				this.buydc.visible=true;
				this.shopObj=obj.IDg_shop;
				if(null==this.shopObj)
				{
					this.bqBtn.visible=false;
					this.buqiTxt.visible=false;
					this.buydc.visible=false;
				}
//				trace("dddd333");	
				var ds:DisplayObject=obj.needItemIcon;
				ds.width=59;
				ds.height=63;
				(this.getChildByName("itemSp") as  IconContainer).addPic(ds,obj.needItemName+"："+obj.needItemDes);
				(this.getChildByName("numTxt") as TextField).text=obj.needItemNum+"/"+obj.targetValue;
				
//				trace("dddd4444");
				if(int(obj.needItemNum)<int(obj.targetValue))
				{
//					trace("dddd5555");
					tmpFlag=false;
					if(null!=this.shopObj)
					{
//						trace("shopObj="+shopObj.itemName);
						this.bqBtn.enabled=true;
						var chaNum:int=int(obj.targetValue)-int(obj.needItemNum);
						pil=Math.ceil(chaNum/shopObj.perNum);
						var shopTips:String=Language.buqiTips;
						while(shopTips.indexOf("@needItemName")!=-1)
						{
							shopTips=shopTips.replace("@needItemName",obj.needItemName);
						}
						while(shopTips.indexOf("@pil")!=-1)
						{
							shopTips=shopTips.replace("@pil",pil);
						}
//						trace("dddd6666");
						shopTips=shopTips.replace("@totalPrice",pil*shopObj.price);
						shopTips=shopTips.replace("@moenytype",moenytype);
						shopTips=shopTips.replace("@itemNum",pil*shopObj.perNum);
						ToolTip_Sprite.getInstance().setTip(this.bqBtn,new MyTipSprite(shopTips),ToolTip_Sprite.DIRECTION_LEFT);
//						trace("dddd7777");
					}
					
				}else{
					this.bqBtn.enabled=false;
				}
				
				//购买按钮
				if(null!=this.shopObj)
				{
					this.buydc.visible=true;
					(this.buydc.getChildByName("txt_itemValue") as TextField).text=String(shopObj.price);
					(this.buydc.getChildByName("txt_itemValue") as TextField).mouseEnabled=false;
					(this.buydc.getChildByName("txt_itemValue") as TextField).selectable=false;
					if(FlatInfo.isOPenQQVip)
					{
						(this.buydc.getChildByName("moneySp") as DisplayObjectContainer).getChildByName("qdian").visible=true;
						(this.buydc.getChildByName("moneySp") as DisplayObjectContainer).getChildByName("obIcon").visible=false;
					}else{
						(this.buydc.getChildByName("moneySp") as DisplayObjectContainer).getChildByName("qdian").visible=false;
						(this.buydc.getChildByName("moneySp") as DisplayObjectContainer).getChildByName("obIcon").visible=true;
					}
					var upBuyTips:String=Language.upBuyBtnTips;
					while(upBuyTips.indexOf("@needItemName")!=-1)
					{
						upBuyTips=upBuyTips.replace("@needItemName",obj.needItemName);
					}
					while(upBuyTips.indexOf("@pil")!=-1)
					{
						upBuyTips=upBuyTips.replace("@pil",1);
					}
					upBuyTips=upBuyTips.replace("@totalPrice",shopObj.price);
					upBuyTips=upBuyTips.replace("@moenytype",moenytype);
					upBuyTips=upBuyTips.replace("@itemNum",shopObj.perNum);
					ToolTip_Sprite.getInstance().setTip(this.buydc,new MyTipSprite(upBuyTips),ToolTip_Sprite.DIRECTION_LEFT);
				}
				
			}else if(Number(obj.countSecondType)==22000){
				this.getChildByName("lvIcon").visible=true;
				ToolTip_Sprite.getInstance().setTip(this.getChildByName("lvIcon"),new MyTipSprite(Language.userLv),ToolTip_Sprite.DIRECTION_LEFT);
				(this.getChildByName("numTxt") as TextField).text=FlatInfo.playLv+"/"+obj.needItemId;
				if(FlatInfo.playLv<int(obj.needItemId))
				{
					tmpFlag=false;
				}
			}else if(Number(obj.countSecondType)==101){
				this.getChildByName("userJb").visible=true;
				ToolTip_Sprite.getInstance().setTip(this.getChildByName("userJb"),new MyTipSprite(Language.jb),ToolTip_Sprite.DIRECTION_LEFT);
				(this.getChildByName("numTxt") as TextField).text=FlatInfo.playMoney+"/"+obj.targetValue;
				if(FlatInfo.playLv<int(obj.needItemId))
				{
					tmpFlag=false;
				}
			}
			
			(this.getChildByName("numTxt") as TextField).textColor	=	tmpFlag?this.oldTxtColor:0xff0000;
			return tmpFlag;
		}
		
		public function reset():void
		{
			this.pil=1;
			this.getChildByName("lvIcon").visible=false;
			this.getChildByName("userJb").visible=false;
			(this.getChildByName("itemSp") as  IconContainer).addPic(null);
			(this.getChildByName("numTxt") as TextField).text="";
			this.bqBtn.visible=false;
			this.buqiTxt.visible=false;
			this.bqBtn.enabled=false;
			this.buydc.visible=false;
			ToolTip_Sprite.getInstance().removeTip(this.bqBtn);
		}
	}
}